﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Data.Objects;

namespace TPA.DataAccess
{
    public interface  ISystemCodeBLL
    {
        void Add(SystemCode code);
        void Remove(int codeid);
        void Remove(SystemCode code);
        void SaveChanges();
        SystemCode Get(int Id);
        /// <summary>
        /// Get root code
        /// </summary>
        /// <returns></returns>
        List<SystemCode> GetRoot();
        List<SystemCode> GetRoot(bool enabled);
        string ConvertToFullText(SystemCode code);
        string ConvertToFullText(int codeid);
        void Refresh();
        void Dispose();
    }

    public class SystemCodeBLL : ISystemCodeBLL
    {

        TVProgamAnalyticEntities myEntities = new TVProgamAnalyticEntities();

        public void Add(SystemCode code)
        {
            myEntities.SystemCodes.AddObject(code);
            //throw new NotImplementedException();
        }

        public void Remove(int codeid)
        {
            //throw new NotImplementedException();
            SystemCode code = Get(codeid);
            Remove(code);
        }

        public void Remove(SystemCode code)
        {
            myEntities.SystemCodes.DeleteObject(code);
            //throw new NotImplementedException();
        }

        public void SaveChanges()
        {
            myEntities.SaveChanges();
            //throw new NotImplementedException();
        }

        public SystemCode Get(int Id)
        {
            return myEntities.SystemCodes.Single(sc => sc.Id == Id);
            //throw new NotImplementedException();
        }

        public List<SystemCode> GetRoot()
        {
            return (from sc in myEntities.SystemCodes
                    where sc.ParentId == -1
                    orderby sc.SortOrder
                    select sc).ToList();
            //throw new NotImplementedException();
        }

        public List<SystemCode> GetRoot(bool enabled)
        {
            return (from sc in myEntities.SystemCodes
                    where sc.ParentId == -1 && sc.Enabled==enabled
                    orderby sc.SortOrder
                    select sc).ToList();
            //throw new NotImplementedException();
        }

        public string ConvertToFullText(SystemCode code)
        {
            return GetParentText(code);
            //throw new NotImplementedException();
        }

        private string GetParentText(SystemCode code)
        {
            string text = "";
            if (code.Parent != null)
            {
                text = code.Parent.Name;
                text=GetParentText(code.Parent)+"\\"+text;
            }
            return text;
        }

        public string ConvertToFullText(int codeid)
        {
            return ConvertToFullText(Get(codeid));
            //throw new NotImplementedException();
        }


        public void Refresh()
        {
            myEntities.Refresh(RefreshMode.StoreWins, myEntities.SystemCodes);
            //throw new NotImplementedException();
        }

        public void Dispose()
        {
            myEntities.Dispose();
        }
    }

}
